package com.mattmckay.server;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.sqs.AmazonSQS;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteQueueRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageRequest;

public class TestAwsClass {
	
	private AmazonS3 s3Test;
	private AmazonSQS sqsTest;
	
	
	public TestAwsClass() throws IOException{
		s3Test = new AmazonS3Client(new PropertiesCredentials(
				Server.class.getResourceAsStream("AwsCredentials.properties")));
		sqsTest = new AmazonSQSClient(new PropertiesCredentials(
				Server.class.getResourceAsStream("AwsCredentials.properties")));
	}

	public byte[] PullS3Object(String objectname, String bucketname) throws IOException{
	S3Object object = s3Test.getObject(bucketname, objectname);	
	return Convert.InputStreamToByteArray(object.getObjectContent());
	}
	
	public void PushS3Object(byte[] image, String message, String bucketname) throws IOException
	{
		InputStream stream = new ByteArrayInputStream(image);
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentLength(image.length);
		meta.setContentType("application/png");
		s3Test.putObject(bucketname,message, stream, meta);
		System.out.println("File has been putted");
		stream.close();
	}
	
	public Message CreateMessage(String messagebody){
		Message message = new Message();
		message.setBody(messagebody);
		return message;
	}
	
	public void CreateBucket(String bucketname)
	{
		s3Test.createBucket(bucketname);
	}
	
	public void DeleteBucket(String bucketname)
	{
		s3Test.deleteBucket(bucketname);
	}
	
	public String SendMessage(String messagebody, String queue) throws InterruptedException{
		sqsTest.sendMessage(new SendMessageRequest(queue, messagebody));
		Thread.sleep(3000);
		return FindMessage(queue, messagebody);
	}
	public String FindMessage(String queue, String messagebody)
	{
		List<Message> messages = GetQueueMessages(queue);
		for(Message message:messages)
		{
			if(message.getBody().equals(messagebody))
			{
				return message.getReceiptHandle();
			}
		}
		return "";
	}
	
	public boolean RunTestOnQueueDelay(String message, String queue, int duration) throws InterruptedException
	{
		int count = 0;
		while(!CheckQueueForMessage(message, queue) && count < duration)
		{
		Thread.sleep(2000);
		count++;
		}
		
		return CheckQueueForMessage(message, queue);
	}
	
	public boolean CheckQueueForMessage(String messagebody, String queue)
	{
		List<Message> messages = GetQueueMessages(queue); 
		for(Message message: messages)
		{
			if(message.getBody().equals(messagebody))
			{
				return true;
			}
		}
		return false;
	}
	public List<Message> GetQueueMessages(String queue){
		ReceiveMessageRequest receivemessagerequest = new ReceiveMessageRequest(queue);
		List<Message> messages = sqsTest.receiveMessage(receivemessagerequest.withMaxNumberOfMessages(receivemessagerequest.getMaxNumberOfMessages())).getMessages();
		return messages;
	}
	
	public void DeleteMessage(String Message, String Queue)
	{
		sqsTest.deleteMessage(new DeleteMessageRequest(Queue, Message));
	}
	
	public void CreateQueue(String queue)
	{
		sqsTest.createQueue(new CreateQueueRequest(queue));
	}
	
	public void DeleteQueue(String queue)
	{
		sqsTest.deleteQueue(new DeleteQueueRequest(queue));
	}
}
